package yxy.game.pm2.active.command.holiday;

import yxy.cherry.data.DataCenter;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.active.module.ActiveModule_节日活动;
import yxy.game.pm2.active.module.DrawStatus;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.Task;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ActiveMessage;
import yxy.game.pm2.module.CurrencyHandle;

import java.util.Collection;

/**
 * Author HYB
 * 领取任务奖励
 */
@MessageType(proto = Command.ActHolidayDrawTaskRewardRequest, classtype = ActiveMessage.ActHolidayDrawTaskRewardRequest.class)
public class CMD_Active_节日_领取任务奖励 extends Command<ActiveMessage.ActHolidayDrawTaskRewardRequest> {


    @Override
    public void execute(MessageSource messageSource, ActiveMessage.ActHolidayDrawTaskRewardRequest request) throws Exception {
        String pcode = messageSource.getTag();
        ActiveMessage.ActHolidayDrawTaskRewardResponse.Builder response = ActiveMessage.ActHolidayDrawTaskRewardResponse.newBuilder();
        String id = request.getId();
        ActiveModule_节日活动 am = ActiveModule_节日活动.get(pcode,false);
        if (am == null) {
            send(messageSource, response, Result.ActNoOpen);
            return;
        }
        DrawStatus cangainreward = am.canGainTaskReward(id);
        if (cangainreward == DrawStatus.cannotget) {
            send(messageSource, response, Result.ActCannotGet);
            return;
        }
        Task task = Task.get(pcode, id);
        am.addTaskActiveNum();
        task.setReward(true);
        task.set();

        yxy.cherry.data.bean.task data = DataCenter.getData(task.getDataId(), yxy.cherry.data.bean.task.class);
        Collection<Currency> cur = Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum());
        if (cur == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        response.addAllDisplay(new CurrencyHandle().packItems(cur));

        //下发道具
        new CurrencyHandle().addCurrency(pcode, CCReason.活动_节日, cur);

        send(messageSource, response, Result.Success);

    }

    private void send(MessageSource messageSource, ActiveMessage.ActHolidayDrawTaskRewardResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
